package com.arouter.demo.homemodule.viewmodel;

import android.os.Build;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.arouter.demo.baselib.router.RouterPath;
import com.arouter.demo.homemodule.BlankFragment;
import com.arouter.demo.homemodule.R;

/**
 * desc:
 * 2018-05-30 14:55
 */
public class HomeVm {

    public void onClickBasicSet(View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_close_router) {

        }
    }

    /**
     * 基本功能
     */
    public void onClickBasicFunc(View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_normal_jump) {
            ARouter.getInstance().build(RouterPath.HOME_ACTIVITY)
                   .navigation();
            return;
        }

        if (viewId == R.id.btn_normal_jump_result) {
            //  ARouter 在Fragment中处理 forResult,注意两点：
            // 1、requestCode 必须大于 0
            // 2、onActivityResult 必须在所依附的Activity 中做处理
            ARouter.getInstance()
                   .build(RouterPath.HOME_ACTIVITY)
                   .navigation((FragmentActivity) view.getContext(), 1);
            return;
        }
        if (viewId == R.id.btn_normal_jump_params) {
            ARouter.getInstance()
                   .build(RouterPath.HOME_ACTIVITY)
                   .withString("name", "老王")
                   .withInt("age", 23)
                   .navigation();

//            通过Uri跳转
//            Uri testUriMix = Uri.parse("arouter://m.aliyun.com/test/activity2");
//            ARouter.getInstance().build(testUriMix)
//                   .withString("key1", "value1")
//                   .navigation();

            return;
        }
        if (viewId == R.id.btn_jump_old_anim) {
            ARouter.getInstance()
                   .build(RouterPath.HOME_ACTIVITY)
                   .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
                   .navigation(view.getContext());// 必须要传context,才有效果
            return;
        }
        if (viewId == R.id.btn_jump_new_anim) {
            if (Build.VERSION.SDK_INT >= 16) {
                ActivityOptionsCompat compat = ActivityOptionsCompat.makeScaleUpAnimation(view,
                        view.getWidth() / 2, view.getHeight() / 2, 0, 0);

                ARouter.getInstance().build(RouterPath.HOME_ACTIVITY)
                       .withOptionsCompat(compat)
                       .navigation();

            } else {
                Toast.makeText(view.getContext(), "API < 16,不支持新版本动画", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        if (viewId == R.id.btn_get_fragment) {

            BlankFragment fragment = (BlankFragment) ARouter.getInstance()
                                                            .build(RouterPath.TEST_FRAGMENT)
                                                            .navigation();

            Toast.makeText(view.getContext(), "找到Fragment:" + fragment.toString(),
                    Toast.LENGTH_SHORT).show();
        }

/*

        if (viewId == R.id.btn_kotlin_jump) {
            // 见Arouter 在Kotlin中的应用

        }
*/

    }

    /**
     * 降级的概念就是，当跳转到某个目标页失败时，可以做处理。这个是Arouter的一大特点
     * 在原始的跳转中，只能是系统级别的处理。
     *
     * @param view
     */
    public void onClickJumpFail(final View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_jump_fail_single) {
            ARouter.getInstance()
                   .build("/xx/xxx")
                   .navigation(view.getContext(), new NavCallback() {
                       @Override
                       public void onFound(Postcard postcard) {
                           Log.e("ARouter", "找到了");
                       }

                       @Override
                       public void onLost(Postcard postcard) {
                           Log.e("ARouter", "找不到了");
                       }

                       @Override
                       public void onArrival(Postcard postcard) {
                           Log.e("ARouter", "跳转完了");
                       }

                       @Override
                       public void onInterrupt(Postcard postcard) {
                           Log.e("ARouter", "被拦截了");
                       }
                   });
            return;
        }

        if (viewId == R.id.btn_jump_fail_all) {
            ARouter.getInstance()
                   .build("/xx/xxx")
                   .navigation();
        }
     /*   if (viewId == R.id.btn_service_fail) {

            // todo
        }
*/

    }


}
